package com.pandas.community.controller;

import com.pandas.community.Utils.CommunityConstant;
import com.pandas.community.Utils.CommunityUtil;
import com.pandas.community.Utils.HostHolder;
import com.pandas.community.entity.Event;
import com.pandas.community.entity.Page;
import com.pandas.community.entity.User;
import com.pandas.community.event.EventProducer;
import com.pandas.community.service.FollowService;
import com.pandas.community.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class FollowController implements CommunityConstant {

    @Autowired
    private FollowService followService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    @Autowired
    private EventProducer eventProducer;

    @RequestMapping(value="/follow", method = RequestMethod.POST)
    @ResponseBody
    public String follow(int entityType, int entityId){
        //加拦截器 / security
        User user = hostHolder.getUser();
        followService.follow(user.getId(), entityType, entityId);

        //触发关注事件
        Event event = new Event()
            .setTopic(TOPIC_FOLLOW)
            .setUserId(hostHolder.getUser().getId())
            .setEntityType(entityType)
            .setEntityId(entityId)
                //目前只能关注人
            .setEntityUserId(entityId);

        eventProducer.fireEvent(event);

        return CommunityUtil.getJSONString(0,"关注成功！");
    }


    @RequestMapping(value="/unfollow", method = RequestMethod.POST)
    @ResponseBody
    public String unfollow(int entityType, int entityId){
        //加拦截器 / security
        User user = hostHolder.getUser();
        followService.unfollow(user.getId(), entityType, entityId);
        return CommunityUtil.getJSONString(0,"已取消关注！");
    }

    @RequestMapping(value="/followee/{targetId}", method = RequestMethod.GET)
    public String getFollowee(@PathVariable("targetId") int targetId, Page page, Model model){
        User target = userService.selectById(targetId);
        if(target == null) throw new RuntimeException("关注者不存在！");
        model.addAttribute("target", target);

        page.setPageCapacity(5);
        page.setPath("/followee/" + targetId);
        page.setInfoAmount((int)followService.getFolloweeCount(targetId, ENTITY_TYPE_USER));


        List<Map<String,Object>> targetFollowee = followService.getFollowees(target.getId(),
                page.getOffset(), page.getPageCapacity());

        //查询当前用户是否关注这些人
        if(targetFollowee != null){
            for(Map<String, Object> followee : targetFollowee){
                User followeeUser = (User)followee.get("followeeUser");
                followee.put("hasFollowed", hasFollowed(followeeUser.getId()));
            }
        }

        model.addAttribute("followees", targetFollowee);

        return "/site/followee";
    }

    @RequestMapping(value="/follower/{targetId}", method = RequestMethod.GET)
    public String getFollower(@PathVariable("targetId") int targetId, Page page, Model model){
        User target = userService.selectById(targetId);
        if(target == null) throw new RuntimeException("粉丝不存在！");
        model.addAttribute("target", target);

        page.setPageCapacity(5);
        page.setPath("/follower/" + targetId);
        page.setInfoAmount((int)followService.getFollowerCount(ENTITY_TYPE_USER,targetId));


        List<Map<String,Object>> targetFollower = followService.getFollowers(target.getId(),
                page.getOffset(), page.getPageCapacity());

        //查询当前用户是否关注这些人
        if(targetFollower != null){
            for(Map<String, Object> follower : targetFollower){
                User followeeUser = (User)follower.get("followerUser");
                follower.put("hasFollowed", hasFollowed(followeeUser.getId()));
            }
        }

        model.addAttribute("followers", targetFollower);

        return "/site/follower";
    }


    //判断当前用户是否关注某个用户
    private boolean hasFollowed(int targetId){
        if(hostHolder.getUser() == null) return false;
        return followService.hasFollowed(hostHolder.getUser().getId(), ENTITY_TYPE_USER, targetId);
    }


}
